package com.loe.ui.api

import android.webkit.JavascriptInterface
import com.loe.mvp.ext_app.addOnItemClickListener
import com.loe.mvp.ext_java.safe
import com.loe.ui.*
import com.loe.ui.view.UiListView
import com.quickjs.JSArray
import com.quickjs.JSFunction
import com.quickjs.JSObject

open class ListViewApi(private val view: UiListView) : ViewApi(view)
{
    @JavascriptInterface
    fun setItemConvert(callback: JSFunction)
    {
        view.setItemConvert()
        { holder, data ->
            val i = holder.adapterPosition - (if (view.headerView != null) 1 else 0)
            callback.call(view.jsObject, JSArray(jsContext).push(jsObject).push(data).push(i))
        }
    }

    /** 加载监听 */
    @JavascriptInterface
    fun setOnLoadData(callback: JSFunction)
    {
        view.setOnLoadData()
        { isRefresh ->
            safe { callback.call(view.jsObject, JSArray(jsContext).push(isRefresh)) }
        }
    }

    /** 加载 */
    @JavascriptInterface
    fun loadData(isRefresh: Boolean) = view.loadData(isRefresh)

    /** 显示刷新 */
    @JavascriptInterface
    fun refresh() = view.refresh()

    /** 元素点击 */
    @JavascriptInterface
    fun addOnItemClickListener(callback: JSFunction)
    {
        view.addOnItemClickListener()
        {
            val data = view.uiAdapter?.getItem(it)
            callback.call(jsObject, JSArray(jsContext).push(data).push(it))
        }
    }

    /** 设置列表 */
    @JavascriptInterface
    fun setDatas(array: JSArray)
    {
        view.uiAdapter?.setNewData(array.toList())
    }

    /** 获取列表 */
    @JavascriptInterface
    fun getDatas(): JSArray
    {
        val jsArray = JSArray(jsContext)
        view.uiAdapter?.data?.forEach { jsArray.push(it) }
        return jsArray
    }

    /** 获取元素 */
    @JavascriptInterface
    fun get(i: Int): JSObject? = view.uiAdapter?.get(i)

    /** 移除元素 */
    @JavascriptInterface
    fun remove(i: Int)
    {
        if (view.headerView != null)
        {
            view.uiAdapter?.remove(i)
            val c = view.uiAdapter?.itemCount ?: 1
            view.uiAdapter?.notifyItemChanged(c - 1)
        } else
        {
            view.uiAdapter?.remove(i)
        }
    }

    /** 清除元素 */
    @JavascriptInterface
    fun clear()
    {
        view.uiAdapter?.data?.clear()
        view.uiAdapter?.notifyDataSetChanged()
    }

    /** 更新元素 */
    @JavascriptInterface
    fun notifyItemChanged(i: Int) = view.uiAdapter?.notifyItemChanged(i)

    /** 更新元素 */
    @JavascriptInterface
    fun notifyDataSetChanged() = view.uiAdapter?.notifyDataSetChanged()

    /** 头布局 */
    @JavascriptInterface
    fun getHeader(): JSObject? = (view.headerView as IUiView?)?.jsObject

    /** 头布局 */
    @JavascriptInterface
    fun getFooter(): JSObject? = (view.footerView as IUiView?)?.jsObject

    @JavascriptInterface
    fun setOnListScroll(callback: JSFunction)
    {
        view.onListScroll = { position, top -> callback.call(jsObject, JSArray(jsContext).push(position).push(top)) }
    }

    @JavascriptInterface
    fun scrollTo(t: String)
    {
        view.scrollTo(0, t.px)
    }

    @JavascriptInterface
    fun scrollToPosition(position: Int)
    {
        view.scrollToPosition(position)
    }

    @JavascriptInterface
    fun smoothScrollToPosition(position: Int)
    {
        view.smoothScrollToPosition(position)
    }

    init
    {
        // 加载成功
        jsObject.registerFun("loadOk")
        {
            if(it.length() > 1)
            {
                view.loadOk(it.getArray(0), it.getBoolean(1))
            }else
            {
                view.loadOk(it.getArray(0), true)
            }
        }

        // 加载失败
        jsObject.registerFun("loadError")
        {
            try
            {
                view.loadError(it.getBoolean(0))
            } catch (e: Exception)
            {
                view.loadError(true)
            }
        }

        // 添加
        jsObject.registerFun("add")
        {
            if (it.length() == 1)
            {
                view.uiAdapter?.addData(it.getObject(0))
            } else
            {
                view.uiAdapter?.addData(it.getInteger(1), it.getObject(0))
            }
        }
    }
}